package com.doubi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.mapper.SysRoleMapper;
import com.doubi.model.sys.TSysRole;
import com.doubi.model.sys.TSysUser;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import com.doubi.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SysRoleService extends ServiceImpl<SysRoleMapper, TSysRole>  {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRolePermissionService sysRolePermissionService;
    @Autowired
    private SysDeptRoleService sysDeptRoleService;
    @Autowired
    private SysUserRoleService sysUserRoleService;


    /**
     * MrDoubi
     * 2019年10月26日 22:35:19
     * 分页权限
     * @param page page 对象
     * @param sysRole 权限bean
     * @return page
     */
    public PageVo<TSysRole> getPage(PageVo<TSysRole> page, TSysRole sysRole){
        QueryWrapper<TSysRole> wrapper = new QueryWrapper<>();
        if(DataUtil.isNotEmpty(sysRole.getRoleName())){
            wrapper.like("role_name",sysRole.getRoleName());
        }
        wrapper.orderByAsc("create_time");
        sysRoleMapper.selectPage(page,wrapper);
        return page;
    }



    /**
     * 功能说明：保存角色
     * @author  MrDoubi 2019-06-27 10:44:01
     * @param sysRole 角色bean
     * @param sysUser 用户bean
     * @return model
     */
    public AppModel saveBean(TSysRole sysRole, TSysUser sysUser) {
        if(DataUtil.isNotEmpty(sysRole.getOid())){//修改
            TSysRole bean = sysRoleMapper.selectById(sysRole.getOid());
            if(!bean.getRoleName().equals(sysRole.getRoleName())){
                QueryWrapper<TSysRole> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("role_name",sysRole.getRoleName());
                if(DataUtil.isNotEmpty(sysRoleMapper.selectOne(queryWrapper))){
                    return new AppModel(-1,"角色标识 已经存在！");
                }
            }
            bean.setRoleSort(sysRole.getRoleSort());
            bean.setRoleName(sysRole.getRoleName());
            bean.setStatus(sysRole.getStatus());
            bean.setUpdateTime(new Date());
            bean.setUpdateBy(sysUser.getOid());
            sysRoleMapper.updateById(bean);
        }else {
            QueryWrapper<TSysRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_name",sysRole.getRoleName());
            if(DataUtil.isNotEmpty(sysRoleMapper.selectOne(queryWrapper))){
                return new AppModel(-1,"角色标识 已经存在！");
            }
            sysRole.setCreateTime(new Date());
            sysRole.setCreateBy(sysUser.getOid());
            sysRole.setDataScope(2);
            sysRoleMapper.insert(sysRole);
        }
        return new AppModel(0,"数据操作成功！",sysRole);
    }


    /**
     * 功能说明：删除角色
     * @author  MrDoubi 2019-01-10 15:21:08
     * @param sysRole bean
     * @return mdel
     */
    public AppModel delBean(TSysRole sysRole) {
        if(DataUtil.isNotEmpty(sysRole.getOid())){
            TSysRole bean = sysRoleMapper.selectById(sysRole.getOid());
            sysRolePermissionService.deleteAllByRoleOid(bean.getOid());
            sysDeptRoleService.deleteAllByRoleOid(bean.getOid());
            sysUserRoleService.deleteAllByRoleOid(bean.getOid());
            sysRoleMapper.deleteById(bean.getOid());
            return AppModel.success();
        }else {
            return AppModel.error();
        }
    }
}
